package de.hwrBerlin.dataMining.view;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;

import de.hwrBerlin.dataMining.constants.ConfigConstants;
import de.hwrBerlin.dataMining.constants.Images;
import de.hwrBerlin.dataMining.constants.UIConstants;
import de.hwrBerlin.dataMining.controller.Controller;
import de.hwrBerlin.dataMining.controller.observer.Observer;
import de.hwrBerlin.dataMining.dataStructure.TableData;
import de.hwrBerlin.dataMining.dataStructure.TreeNode;
import de.hwrBerlin.dataMining.view.dialog.AboutDialogue;
import de.hwrBerlin.dataMining.view.dialog.ConfigurationDialog;
import de.hwrBerlin.dataMining.view.dialog.ManualDialog;

public class View implements Observer{
    
    private Shell _shell;
    private Display _display;
    private Controller _controller;
    private TableView _tableView;

    private static TabFolder _tabFolder;
    private static TabItem _tableItem;
    private static TabItem _interativeTreeItem;
    private static TabItem _automaticTreeItem;
	private MenuItem _subMenuItemExport;
    
    
    
    public View(Controller pController) {
        	
        Display.setAppName(UIConstants.TITLE);
        
        _controller = pController;
        _controller.addObserverToModel(this);
        
        _display = new Display();
        _shell = new Shell();
        
        FillLayout shellLayout = new FillLayout();
        shellLayout.marginHeight = 5;
        shellLayout.marginWidth = 5;
        _shell.setLayout(shellLayout);
        _shell.setImages(Images.LOGO_ICONS);
        _shell.setText(UIConstants.TITLE);
        
        createMenu();
        createTabs();
        
        _controller.loadTable(new TableData());
        
        updateEnable(_controller.getModel().getTable());
        
        _shell.open();
        while (!_shell.isDisposed()) {
            if (!_display.readAndDispatch()) {
                _display.sleep();
            }
        }
        _display.dispose();
    }
    
    
    
    private void createMenu() {
        Menu menuBar = new Menu(_shell, SWT.BAR);
        
        createFileMenu(menuBar);
        createHelpMenu(menuBar);
        
        _shell.setMenuBar(menuBar);
    }
    
    
    
    private void createFileMenu(Menu pParent) {
        MenuItem fileItem = new MenuItem(pParent, SWT.CASCADE);
        fileItem.setText(UIConstants.FILE);
        
        Menu fileMenu = new Menu(_shell, SWT.DROP_DOWN);
        fileItem.setMenu(fileMenu);
        
        // Import ---------------------------------------------------------------------------------------------------------------------------------------
        MenuItem subMenuItemImport = new MenuItem(fileMenu, SWT.CASCADE);
        subMenuItemImport.setText(UIConstants.SUB_MENU_IMPORT);
      
        Menu subMenuImport = new Menu(_shell, SWT.DROP_DOWN);
        subMenuItemImport.setMenu(subMenuImport);
        
        MenuItem importTableItem = new MenuItem(subMenuImport, SWT.PUSH);
        importTableItem.setText(UIConstants.IMPORT_TABLE);
        importTableItem.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent pE) {
                FileDialog fileDialog = new FileDialog(_shell, SWT.OPEN);
                fileDialog.setFilterExtensions(UIConstants.TABLE_IMPORT_EXTENSIONS);
                fileDialog.setFilterNames(UIConstants.TABLE_IMPORT_EXTENSIONS_NAMES);
                if(_controller.loadTable(fileDialog.open())){
                    _tabFolder.setSelection(_tableItem);
                }
            }
        });
        
        MenuItem importTreeItem = new MenuItem(subMenuImport, SWT.PUSH);
        importTreeItem.setText(UIConstants.IMPORT_TREE);
        importTreeItem.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent pE) {
                FileDialog fileDialog = new FileDialog(_shell, SWT.OPEN);
                fileDialog.setFilterExtensions(UIConstants.TREE_IMPORT_EXTENSIONS);
                fileDialog.setFilterNames(UIConstants.TREE_IMPORT_EXTENSIONS_NAMES);
                if(_controller.openInteractiveTree(fileDialog.open())){
                    _tabFolder.setSelection(_interativeTreeItem);
                }
                
                
            }
        });

        // Export ---------------------------------------------------------------------------------------------------------------------------------------
        _subMenuItemExport = new MenuItem(fileMenu, SWT.CASCADE);
        _subMenuItemExport.setText(UIConstants.SUB_MENU_EXPORT);
      
        Menu subMenuExport = new Menu(_shell, SWT.DROP_DOWN);
        _subMenuItemExport.setMenu(subMenuExport);
        
        MenuItem exportTableItem = new MenuItem(subMenuExport, SWT.PUSH);
        exportTableItem.setText(UIConstants.EXPORT_TABLE);
        exportTableItem.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent pE) {
                FileDialog fileDialog = new FileDialog(_shell, SWT.SAVE);
                
                fileDialog.setFilterExtensions(UIConstants.TABLE_EXPORT_EXTENSIONS);
                fileDialog.setFilterNames(UIConstants.TABLE_EXPORT_EXTENSIONS_NAMES);
                _controller.saveTable(fileDialog.open());
            }
        });
        
        MenuItem exportInteractiveTreeItem = new MenuItem(subMenuExport, SWT.PUSH);
        exportInteractiveTreeItem.setText(UIConstants.EXPORT_INTERACTIVE_TREE);
        exportInteractiveTreeItem.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent pE) {
                FileDialog fileDialog = new FileDialog(_shell, SWT.SAVE);
                fileDialog.setFilterExtensions(UIConstants.TREE_EXPORT_EXTENSIONS);
                fileDialog.setFilterNames(UIConstants.TREE_EXPORT_EXTENSIONS_NAMES);
                _controller.saveInteractiveTree(fileDialog.open());
            }
        });
        
        MenuItem exportAutomaticTreeItem = new MenuItem(subMenuExport, SWT.PUSH);
        exportAutomaticTreeItem.setText(UIConstants.EXPORT_AUTOMATIC_TREE);
        exportAutomaticTreeItem.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent pE) {
                FileDialog fileDialog = new FileDialog(_shell, SWT.SAVE);
                fileDialog.setFilterExtensions(UIConstants.TREE_EXPORT_EXTENSIONS);
                fileDialog.setFilterNames(UIConstants.TREE_EXPORT_EXTENSIONS_NAMES);
                _controller.saveAutomaticTree(fileDialog.open());
            }
        });

        // config ---------------------------------------------------------------------------------------------------------------------------------------
        MenuItem configurationItem = new MenuItem(fileMenu, SWT.PUSH);
        configurationItem.setText(UIConstants.CONFIGURATION);
        configurationItem.addSelectionListener(new SelectionAdapter() {
        	@Override
        	public void widgetSelected(SelectionEvent pE) {
        		openConfigurations();
        	}
        	
        });

        // exit ---------------------------------------------------------------------------------------------------------------------------------------
        MenuItem exitItem = new MenuItem(fileMenu, SWT.PUSH);
        exitItem.setText(UIConstants.EXIT);
        exitItem.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent pE) {
            	if (!ConfigConstants.SURE_TO_EXIT) {
            		 MessageBox messageBox = new MessageBox(_shell, SWT.ICON_WARNING | SWT.OK | SWT.CANCEL);
                     messageBox.setText(UIConstants.EXIT);
                     messageBox.setMessage(UIConstants.SURE_TO_EXIT_MESSAGE);
                     
                     if (messageBox.open() == SWT.OK) {
                         System.exit(0);
                     }
				} else {
					System.exit(0);
				}
               
            }
        });
    }
    
    
    
    private void createHelpMenu(Menu pParent) {
        MenuItem helpItem = new MenuItem(pParent, SWT.CASCADE);
        helpItem.setText(UIConstants.HELP);

        Menu helpMenu = new Menu(_shell, SWT.DROP_DOWN);
        helpItem.setMenu(helpMenu);
        
        MenuItem manualItem = new MenuItem(helpMenu, SWT.PUSH);
        manualItem.setText(UIConstants.MANUAL);
        manualItem.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent pE) {
                openManual();
            }
        });
        
        
        MenuItem aboutItem = new MenuItem(helpMenu, SWT.PUSH);
        aboutItem.setText(UIConstants.ABOUT);
        aboutItem.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent pE) {
                openAboutDialog();
            }
        });
    }
    
    
    
    private void openManual() {
        Shell manualShell = new Shell(_display);
        
        manualShell.setLayout(new FillLayout());
        manualShell.setImages(Images.LOGO_ICONS);
        manualShell.setText(UIConstants.MANUAL);
        new ManualDialog(manualShell, SWT.NONE);
        
        manualShell.pack();
        manualShell.setSize(1000, 600);
        manualShell.open();
    }
    
    
    
    private void openAboutDialog() {
        Shell aboutShell = new Shell(_display);
        
        aboutShell.setLayout(new FillLayout());
        aboutShell.setImages(Images.LOGO_ICONS);
        aboutShell.setText(UIConstants.ABOUT);
        
        new AboutDialogue(aboutShell, SWT.NONE);
        
        aboutShell.pack();
        aboutShell.open();
    }
    
    
    
    private void openConfigurations() {
        if (new ConfigurationDialog(_shell, SWT.NONE, _controller, _tableView).open() == SWT.OK && _controller.getModel().getTable() != null) {
            _controller.updateTrees();
            _tableView.updateAllTable(_controller.getModel().getTable());
        }
    }
    
    
    
    private void createTabs() {
        _tabFolder = new TabFolder(_shell, SWT.NONE);
        
        _tableItem = new TabItem(_tabFolder, SWT.NONE);
        _tableItem.setText(UIConstants.TABLE_TAB_NAME);
        _tableView = new TableView(_tabFolder, SWT.NONE, _tableItem, _controller);
        
        _interativeTreeItem = new TabItem(_tabFolder, SWT.NONE);
        _interativeTreeItem.setText(UIConstants.INTERACTIV_TREE_TAB_NAME);
        new TreeView(_tabFolder, SWT.NONE, _interativeTreeItem, true, _controller);
        
        _automaticTreeItem = new TabItem(_tabFolder, SWT.NONE);
        _automaticTreeItem.setText(UIConstants.AUTOMATIC_TREE_TAB_NAME);
        new TreeView(_tabFolder, SWT.NONE, _automaticTreeItem, false, _controller);
        
        _tabFolder.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                if (!_controller.isTableTreeable()) {
                    _tabFolder.setSelection(_tableItem);
                }
            }
        });
    }
    
    
    
    private void updateEnable(TableData pTableData){
        if (_controller.isTableTreeable(pTableData)) {
            _subMenuItemExport.setEnabled(true);
            _interativeTreeItem.setText(UIConstants.INTERACTIV_TREE_TAB_NAME);
            _automaticTreeItem.setText(UIConstants.AUTOMATIC_TREE_TAB_NAME);
        }
        else {
            _subMenuItemExport.setEnabled(false);
            _interativeTreeItem.setText(UIConstants.INTERACTIV_TREE_TAB_NAME + " (" + UIConstants.DISABLE_TAB + ")");
            _automaticTreeItem.setText(UIConstants.AUTOMATIC_TREE_TAB_NAME + " (" + UIConstants.DISABLE_TAB + ")");
        }
    }



	@Override
	public void updateAllTable(TableData pTableData) {
	    updateEnable(pTableData);
	}



	@Override
	public void updateTree(TreeNode pTree) {
		//NOI
	}



	@Override
	public void updateAllInteractiveTree(TreeNode pTree) {
		//NOI
	}



	@Override
	public void updateAllAutomaticTree(TreeNode pTree) {
		//NOI
	}
}
